#!/bin/bash
backupDir=/apps/redis/backup/redis-rbd
dataDir=/apps/redis/data
file=dump.rdb
password=
cmd=/apps/redis/bin/redis-cli
user=redis

#使redis执行bgsave
if [ -z ${password} ];then
  ${cmd} -h 127.0.0.1 bgsave
  #获取bgsave的状态
  result=$(${cmd} -h 127.0.0.1 info Persistence | grep rdb_bgsave_in_progress | sed -rn 's/.*:([0-9]+).*/\1/p')
  until [ $result -eq 0 ] ;do
    sleep 1
    result=$(${cmd} -h 127.0.0.1 --no-auth-warning info Persistence | grep rdb_bgsave_in_progress | sed -rn 's/.*:([0-9]+).*/\1/p')
  done
else
  ${cmd} -h 127.0.0.1 -a "${password}" --no-auth-warning bgsave
  #获取bgsave的状态
  result=$(${cmd} -h 127.0.0.1 -a ${password} --no-auth-warning info Persistence | grep rdb_bgsave_in_progress | sed -rn 's/.*:([0-9]+).*/\1/p')
  until [ $result -eq 0 ] ;do
    sleep 1
    result=$(${cmd} -h 127.0.0.1 -a ${password} --no-auth-warning info Persistence | grep rdb_bgsave_in_progress | sed -rn 's/.*:([0-9]+).*/\1/p')
  done
fi

localtime=$(date +%F_%H-%M-%S)

#判断备份的文件是否存在，如果不存在备份文件夹就创建并修改文件夹属性
[ -e ${backupDir} ] || { mkdir -p ${backupDir} ; chown -R ${user}.$(user) ${backupDir}; }
mv ${dataDir}/${file} ${backupDir}/dump_6379-${localtime}.rdb
